package java6699.lesson14;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Scanner;

/**
 * @program: java_zzx
 * @description:
 * @author: zzx
 * @create: 2022-12-07 18:21
 **/
public class BaseServer6699 {
    public static void main(String[] args) throws IOException {
        //创建ServerSocket对象
        ServerSocket server = null;
        try {
            //注册（绑定）端口号
            server = new ServerSocket(8086);
            System.out.println("等待客户电话...");
            //调用accept()方法监听是否有客户端发送连接请求
            Socket socket = server.accept();
            //接收信息：从Socket对象的输入流中接收客户端传来的信息，显示在控制台上
            Scanner scanner = new Scanner(socket.getInputStream());
            System.out.println("接听电话：" + scanner.nextLine());
            //发送信息：通过Socket对象的输出流向客户端发送信息
            PrintStream printer = new PrintStream(socket.getOutputStream());
            printer.println("212106699周梓希。回复 贝贝");

            //关闭
            socket.close();
            server.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
